package ru.develbureau.server;

import java.util.List;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.develbureau.server.model.ClientRequestEntity;
import ru.develbureau.server.persistence.EMF;

public class AdminServlet extends HttpServlet{
	private static final Logger log = Logger.getLogger(AdminServlet.class.getName());
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest request, HttpServletResponse response){
		EntityManager em = null;
		try {
			em = EMF.get().createEntityManager();
			String _class_ = ClientRequestEntity.class.getName();
			Query query = em.createQuery("SELECT cr FROM " + _class_ + " cr ORDER BY cr.publishDate DESC");
			List<ClientRequestEntity> result = (List<ClientRequestEntity>)query.getResultList();
			request.setAttribute("clientRequests", result);
			
			request.getRequestDispatcher("/admin.jsp").forward(request, response);
		} 
		catch (Exception e) {
			log.throwing("Error while getting ClientRequests", e.getMessage(), e);
		}
		finally{
			EMF.close(em);
		}
	}
}
